package es.ats.codington.festival.services;

import java.sql.Connection;
import java.sql.SQLException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import es.ats.codington.festival.daos.DAOManager;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.interfaces.daos.IVisitorDAO;
import es.ats.codington.festival.interfaces.services.IVisitorService;


public class VisitorService implements IVisitorService{

	Connection connection=null;
	
	private IVisitorDAO visitorDAO;
	
	/**
	 * 
	 */
	public VisitorService(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext(  "beans.xml");  
		DAOManager daoManager = (DAOManager) ctx.getBean("daoManager");
		this.visitorDAO = daoManager.getUserDAO();
	}
	

	public boolean registerNewVisitor(Visitor u) throws SQLException, ClassNotFoundException{
		
		boolean bool = false;
		
		try {
			bool = visitorDAO.registerNewVisitorDAO(u);
		} catch(Exception e) {
			e.getMessage();
			return false;
			}
		return bool;
	}
	
	/**
	 * 
	 * @param u
	 * @return 
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public boolean updateVisitor(Visitor u) throws SQLException, ClassNotFoundException{
		
		boolean bool=false;
		
		try {
			bool=visitorDAO.updateVisitorDAO(u);
						
		}catch (Exception ee) {
			ee.getMessage();
			return false;
		} 
			return bool;
	}
	
	/**
	 * 
	 * @param u
	 * @return 
	 * @throws SQLException
	 */
	
	public int getIdVisitor(Visitor u) throws SQLException{
		
		int idVisitor = 0;
		
		try {
			idVisitor=visitorDAO.getIdVisitorDAO(u);
						
		}catch (Exception ee) {
			ee.getMessage();
		} 

		return idVisitor;
	}
	
	/**
	 * 
	 * @param u
	 * @return 
	 * @throws SQLException
	 */
	
	public boolean login(Visitor u) throws SQLException{
		
		boolean bool=false;
		
		try {
			bool=visitorDAO.loginDAO(u);
						
		}catch (Exception ee) {
			ee.getMessage();
			return false;
		} 
		return bool;
	}
	
	/**
	 * 
	 * @param u
	 * @return 
	 * @throws SQLException
	 */
	public boolean isAdmin(Visitor u) throws SQLException{
		
		boolean bool=false;
		
		try {
			bool=visitorDAO.isAdminDAO(u);
						
		}catch (Exception ee) {
			ee.getMessage();
			return false;
		} 
		return bool;
	}
	
	/**
	 * 
	 * @param u
	 * @return 
	 * @throws SQLException
	 */
	
	public boolean unregisterVisitor(Visitor u) throws SQLException{
		
		boolean bool=false;
		
		try {
			bool=visitorDAO.unregisterVisitorDAO(u);
						
		}catch (Exception ee) {
			ee.getMessage();
			return false;
		} 
		return bool;
	}


	public Visitor getVisitor(Visitor u) throws SQLException{
		
		try {
			u=visitorDAO.getVisitorDAO(u);
						
		}catch (Exception ee) {
			ee.getMessage();
		} 

		return u;
	}
	
}
